package com.lxb.myforum.service.spec

import java.util.ArrayList

import javax.persistence.criteria.CriteriaBuilder
import javax.persistence.criteria.CriteriaQuery
import javax.persistence.criteria.Predicate
import javax.persistence.criteria.Root

import org.apache.commons.lang3.StringUtils
import org.springframework.data.jpa.domain.Specification

import com.lxb.myforum.pojo.Role

class RoleSpec(private val role: Role) : Specification<Role> {

    override fun toPredicate(root: Root<Role>, query: CriteriaQuery<*>, cb: CriteriaBuilder): Predicate? {
        val predicates = ArrayList<Predicate>()
        if (StringUtils.isNotBlank(role.name)) {
            predicates.add(cb.like(root.get("name"), "%" + role.name + "%"))
        }
        query.where(*predicates.toTypedArray())
        return null
    }
}
